home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp95 / freyja_t.z / freyja_t / word.c < prev    next >
C/C++ Source or Header  |  1992-04-14  |  10KB  |  563 lines

  1. /* WORD.C -- Word-Oriented Commands
  2.  
  3.     Written July 1991 by Craig A. Finseth
  4.     Copyright 1991 by Craig A. Finseth
  5. */
  6.  
  7. #include "freyja.h"
  8.  
  9. void W_Fill();        /* void */
  10. FLAG W_IsClose();    /* void */
  11. FLAG W_IsCNumber();    /* void */
  12. FLAG W_IsNLPunct();    /* void */
  13. FLAG W_IsParaEnd();    /* void */
  14. FLAG W_IsSentEnd();    /* void */
  15. FLAG W_IsToken();    /* void */
  16. void W_MoveBlock();    /* struct mark *from, struct mark *to */
  17. void W_ToWord();    /* void */
  18.  
  19. /* ------------------------------------------------------------ */
  20.  
  21. /* "Rotate" the case of the current word L -> C -> U -> L. */
  22.  
  23. void
  24. WCaseRotate()
  25.     {
  26.     FLAG waslower;
  27.  
  28.     BMarkToPoint(cwin->point);
  29.     if (BIsEnd() || !W_IsToken()) WWordB();
  30.     while (!BIsEnd() && !xisalpha(BGetChar())) BMoveBy(1);
  31.     if (BIsEnd()) {
  32.         BPointToMark(cwin->point);
  33.         return;
  34.         }
  35.  
  36.     if (xislower(BGetChar())) {
  37.         BCharChange(xtoupper(BGetChar()));
  38.         waslower = FALSE;
  39.         }
  40.     else    {
  41.         BMoveBy(1);
  42.         waslower = xislower(BGetChar());
  43.         BMoveBy(-1);
  44.         }
  45.     while (!BIsEnd() && W_IsToken()) {
  46.         BCharChange(waslower ? xtoupper(BGetChar()) :
  47.             xtolower(BGetChar()));
  48.         }
  49.     BPointToMark(cwin->point);
  50.     }
  51.  
  52.  
  53. /* ------------------------------------------------------------ */
  54.  
  55. /* Move backward one number. */
  56.  
  57. void
  58. WNumB()
  59.     {
  60.     MoveToB(W_IsCNumber);
  61.     MovePastB(W_IsCNumber);
  62.     }
  63.  
  64.  
  65. /* ------------------------------------------------------------ */
  66.  
  67. /* Move forward one number. */
  68.  
  69. void
  70. WNumF()
  71.     {
  72.     MoveToF(W_IsCNumber);
  73.     MovePastF(W_IsCNumber);
  74.     }
  75.  
  76.  
  77. /* ------------------------------------------------------------ */
  78.  
  79. /* Mark the current number. */
  80.  
  81. void
  82. WNumMark()
  83.     {
  84.     MovePastB(W_IsCNumber);
  85.     BMarkToPoint(mark);
  86.     MovePastF(W_IsCNumber);
  87.     }
  88.  
  89.  
  90. /* ------------------------------------------------------------ */
  91.  
  92. /* Move backward one paragraph. */
  93.  
  94. void
  95. WParaB()
  96.     {
  97.     GoToNotGrayB();
  98.     if (cbuf->c.fill != 'W') {
  99.         while (SearchNLB()) {
  100.             BMoveBy(1);
  101.             if (W_IsParaEnd()) break;
  102.             BMoveBy(-1);
  103.             }
  104.         }
  105.     else    BSearchB(NL, NL);
  106.     GoToNotGrayF();
  107.     }
  108.  
  109.  
  110. /* ------------------------------------------------------------ */
  111.  
  112. /* Move forward one paragraph. */
  113.  
  114. void
  115. WParaF()
  116.     {
  117.     GoToNotGrayF();
  118.     if (cbuf->c.fill != 'W')
  119.         while (SearchNLF() && !W_IsParaEnd()) ;
  120.     else    BSearchF(NL, NL);
  121.     GoToNotGrayB();
  122.     }
  123.  
  124.  
  125. /* ------------------------------------------------------------ */
  126.  
  127. /* Fill the paragraph. */
  128.  
  129. void
  130. WParaFill()
  131.     {
  132.     if (cbuf->c.fill != 'W')
  133.         W_Fill();
  134.     else    WWrap();
  135.     uarg = 0;
  136.     }
  137.  
  138.  
  139. /* ------------------------------------------------------------ */
  140.  
  141. /* Set the point and mark around paragraph. */
  142.  
  143. void
  144. WParaMark()
  145.     {
  146.     BMoveBy(-1);
  147.     WParaF();
  148.     BMarkToPoint(mark);
  149.     WParaB();
  150.     }
  151.  
  152.  
  153. /* ------------------------------------------------------------ */
  154.  
  155. /* Move backwards one sentence. */
  156.  
  157. void
  158. WSentB()
  159.     {
  160.     FLAG waspend;
  161.  
  162.     WWordB();
  163.     do    {
  164.         MoveToB(W_IsNLPunct);
  165.         if (BIsStart()) break;
  166.         BMoveBy(-1);
  167.         if (IsNL()) {
  168.             BMoveBy(1);
  169.             waspend = W_IsParaEnd();
  170.             BMoveBy(-1);
  171.             if (waspend) GoToNotGrayF();
  172.             }
  173.         else    {
  174.             waspend = W_IsSentEnd();
  175.             if (waspend && !BIsStart())
  176.                 MoveToF(W_IsToken);
  177.             else    MoveToB(W_IsToken);
  178.             if (waspend) GoToGrayB();
  179.             }
  180.         } while (!waspend);
  181.     }
  182.  
  183.  
  184. /* ------------------------------------------------------------ */
  185.  
  186. /* Delete the previous sentence. */
  187.  
  188. void
  189. WSentBD()
  190.     {
  191.     BMarkToPoint(cwin->point);
  192.     WSentB();
  193.     RKillToMark(cwin->point, BACKWARD);
  194.     }
  195.  
  196.  
  197. /* ------------------------------------------------------------ */
  198.  
  199. /* Move forward a sentence. */
  200.  
  201. void
  202. WSentF()
  203.     {
  204.     for (;;) {
  205.         WWordF();
  206.         MoveToF(W_IsNLPunct);
  207.         if (BIsEnd()) break;
  208.         if (IsNL()) {
  209.             BMoveBy(1);
  210.             if (W_IsParaEnd()) break;
  211.             BMoveBy(-1);
  212.             }
  213.         else if (W_IsSentEnd()) break;
  214.         }
  215.     }
  216.  
  217.  
  218. /* ------------------------------------------------------------ */
  219.  
  220. /* Delete the following sentence. */
  221.  
  222. void
  223. WSentFD()
  224.     {
  225.     BMarkToPoint(cwin->point);
  226.     if (uarg == 0) {
  227.         WSentB();
  228.         RKillToMark(cwin->point, BACKWARD);
  229.         }
  230.     else    {
  231.         WSentF();
  232.         RKillToMark(cwin->point, FORWARD);
  233.         }
  234.     }
  235.  
  236.  
  237. /* ------------------------------------------------------------ */
  238.  
  239. /* Move backwards one word. */
  240.  
  241. void
  242. WWordB()
  243.     {
  244.     MoveToB(W_IsToken);
  245.     MovePastB(W_IsToken);
  246.     }
  247.  
  248.  
  249. /* ------------------------------------------------------------ */
  250.  
  251. /* Delete the previous word. */
  252.  
  253. void
  254. WWordBD()
  255.     {
  256.     BMarkToPoint(cwin->point);
  257.     WWordB();
  258.     RKillToMark(cwin->point, BACKWARD);
  259.     }
  260.  
  261.  
  262. /* ------------------------------------------------------------ */
  263.  
  264. /* Capitalize the following word. */
  265.  
  266. void
  267. WWordCap()
  268.     {
  269.     W_ToWord();
  270.     if (BIsEnd()) return;
  271.     BCharChange(xtoupper(BGetChar()));
  272.     if (W_IsToken()) WWordLow();
  273.     }
  274.  
  275.  
  276. /* ------------------------------------------------------------ */
  277.  
  278. /* Move foward one word. */
  279.  
  280. void
  281. WWordF()
  282.     {
  283.     MoveToF(W_IsToken);
  284.     MovePastF(W_IsToken);
  285.     }
  286.  
  287.  
  288. /* ------------------------------------------------------------ */
  289.  
  290. /* Delete the following word. */
  291.  
  292. void
  293. WWordFD()
  294.     {
  295.     BMarkToPoint(cwin->point);
  296.     WWordF();
  297.     RKillToMark(cwin->point, FORWARD);
  298.     }
  299.  
  300.  
  301. /* ------------------------------------------------------------ */
  302.  
  303. /* Lowercase the following word. */
  304.  
  305. void
  306. WWordLow()
  307.     {
  308.     W_ToWord();
  309.     while (!BIsEnd() && W_IsToken()) {
  310.         BCharChange(xtolower(BGetChar()));
  311.         }
  312.     }
  313.  
  314.  
  315. /* ------------------------------------------------------------ */
  316.  
  317. /* Transpose the adjoining words. */
  318.  
  319. void
  320. WWordTran()
  321.     {
  322.     struct mark *mptr;
  323.  
  324.     MoveToF(W_IsToken);
  325.     if (BIsEnd()) return;
  326.     mptr = BMarkCreate();
  327.     MovePastF(W_IsToken);
  328.     BMarkToPoint(cwin->point);
  329.     BPointToMark(mptr);
  330.     MoveToB(W_IsToken);
  331.     W_MoveBlock(mptr, cwin->point);
  332.  
  333.     MovePastB(W_IsToken);
  334.     W_MoveBlock(mptr, cwin->point);
  335.     BMarkDelete(mptr);
  336.     BPointToMark(cwin->point);
  337.     }
  338.  
  339.  
  340. /* ------------------------------------------------------------ */
  341.  
  342. /* Uppercase the following word. */
  343.  
  344. void
  345. WWordUp()
  346.     {
  347.     W_ToWord();
  348.     while (!BIsEnd() && W_IsToken()) {
  349.         BCharChange(xtoupper(BGetChar()));
  350.         }
  351.     }
  352.  
  353.  
  354. /* ------------------------------------------------------------ */
  355.  
  356. /* Refill the current paragraph in wrap mode. */
  357.  
  358. void
  359. WWrap()
  360.     {
  361.     char markstat = NUL;
  362.     int col;
  363.     int chr;
  364.     int wid;
  365.     int lastlen = 0;
  366.  
  367.     BMarkToPoint(cwin->point);
  368.     if (BSearchB(NL, SNL)) BMoveBy(1);    /* to start of paragraph */
  369.  
  370.     wid = 0;
  371.     for (col = 0; !BIsEnd(); col++, wid++) {
  372.         chr = BGetCharAdv();
  373.         if (chr == SP || chr == TAB) {
  374.             if (markstat == NL && lastlen + col <
  375.                  cbuf->c.right_margin) {
  376.                 BMoveBy(-wid - 1);
  377.                 BCharChange(SP);
  378.                 BMoveBy(wid + 1);
  379.                 col += lastlen + 1;
  380.                 }
  381.             wid = 0;
  382.             markstat = SP;
  383.             }
  384.         else if (chr == NL) {
  385.             break;
  386.             }
  387.         else if (chr == SNL) {
  388.             if (markstat == NL && lastlen + col <
  389.                  cbuf->c.right_margin) {
  390.                 BMoveBy(-wid - 1);
  391.                 BCharChange(SP);
  392.                 BMoveBy(wid + 1);
  393.                 col += lastlen;
  394.                 }
  395.             wid = 0;
  396.             markstat = NL;
  397.             lastlen = col;
  398.             col = -1;
  399.             }
  400.             /* printing character */
  401.         else if (markstat == SP && col >= cbuf->c.right_margin) {
  402.             BMoveBy(-wid - 1);
  403.             chr = BGetChar();
  404.             if (chr != SP && chr != TAB) {
  405.                 BMoveBy(-1);
  406.                 wid++;
  407.                 }
  408.             BCharChange(SNL);
  409.             BMoveBy(wid + 1);
  410.             markstat = NUL;
  411.             col = wid;
  412.             wid = 0;
  413.             }
  414.         }
  415.     BPointToMark(cwin->point);
  416.     }
  417.  
  418.  
  419. /* ------------------------------------------------------------ */
  420.  
  421. /* Fill the current paragraph in hard newline mode. */
  422.  
  423. void
  424. W_Fill()
  425.     {
  426.     struct mark *endptr;
  427.     struct mark *mptr;
  428.     FLAG isnl;
  429.  
  430.     mptr = BMarkCreate();
  431.  
  432.     CLineA();
  433.     WParaF();
  434.     endptr = BMarkCreate();
  435.     WParaB();
  436.  
  437.     while (BIsBeforeMark(endptr)) {
  438.         GoToGrayF();
  439.         if (BGetCol() > cbuf->c.right_margin) {
  440.             GoToGrayB();
  441.             BMoveBy(-1);
  442.             BCharChange(NL);
  443.             BInsSpaces(cbuf->c.left_margin);
  444.             GoToGrayF();
  445.             }
  446.         MovePastF(IsWhite);
  447.         if (IsNL() && BIsBeforeMark(endptr)) {
  448.             BCharChange(SP);
  449.             WDelFWhite();
  450.             }
  451.         }
  452.     BPointToMark(mptr);
  453.     BMarkDelete(endptr);
  454.     BMarkDelete(mptr);
  455.     }
  456.  
  457.  
  458. /* ------------------------------------------------------------ */
  459.  
  460. /* Do we have a closing character? */
  461.  
  462. FLAG
  463. W_IsClose()
  464.     {
  465.     return(*sindex(")]}\"'", BGetChar()) != NUL);
  466.     }
  467.  
  468.  
  469. /* ------------------------------------------------------------ */
  470.  
  471. /* Check for a calculator number digit. */
  472.  
  473. FLAG
  474. W_IsCNumber()
  475.     {
  476.     return(*sindex("0123456789.,-#abcdefABCDEF", BGetChar()) != NUL);
  477.     }
  478.  
  479.  
  480. /* ------------------------------------------------------------ */
  481.  
  482. /* Check for Newline or punctuation */
  483.  
  484. FLAG
  485. W_IsNLPunct()
  486.     {
  487.     return(IsNL() || *sindex(".?!", BGetChar()) != NUL);
  488.     }
  489.  
  490.  
  491. /* ------------------------------------------------------------ */
  492.  
  493. /* Check for the end of a paragraph. */
  494.  
  495. FLAG
  496. W_IsParaEnd()
  497.     {
  498.     return(BIsEnd() || IsGray() || *sindex(".@", BGetChar()) != NUL);
  499.     }
  500.  
  501.  
  502. /* ------------------------------------------------------------ */
  503.  
  504. /* Check for the end of a sentence.  This routine assumes that it
  505. starts at a sentence end (e.g., '.').  It then skips over as many of
  506. ')}]"' or "'" as it finds, and tells you whether you wind up at a
  507. whitespace character. */
  508.  
  509. FLAG
  510. W_IsSentEnd()
  511.     {
  512.     BMoveBy(1);
  513.     MovePastF(W_IsClose);
  514.     return(BIsEnd() || IsGray());
  515.     }        
  516.  
  517.  
  518. /* ------------------------------------------------------------ */
  519.  
  520. /* Tell if current char is part of a token */
  521.  
  522. FLAG
  523. W_IsToken()
  524.     {
  525.     return(xisalnum(BGetChar()));
  526.     }
  527.  
  528.  
  529. /* ------------------------------------------------------------ */
  530.  
  531. /* Move a block of characters between Point and From to before To.
  532. Assume Point is before From */
  533.  
  534. void
  535. W_MoveBlock(from, to)
  536.     struct mark *from;
  537.     struct mark *to;
  538.     {
  539.     int chr;
  540.  
  541.     while (BIsBeforeMark(from)) {
  542.         chr = BGetChar();
  543.         BMarkSwap(to);
  544.         BInsChar(chr);
  545.         BMarkSwap(to);
  546.         BCharDelete(1);
  547.         }
  548.     }
  549.  
  550.  
  551. /* ------------------------------------------------------------ */
  552.  
  553. /* Move to the beginning of a word. */
  554.  
  555. void
  556. W_ToWord()
  557.     {
  558.     MoveToF(W_IsToken);
  559.     }
  560.  
  561.  
  562. /* end of WORD.C -- Word-Oriented Commands */
  563.